package cn.iocoder.yudao.module.system.service.template;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.template.vo.PersonalTemplateBaseVO;
import cn.iocoder.yudao.module.system.controller.admin.template.vo.PersonalTemplateCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.template.vo.PersonalTemplateRespVO;
import cn.iocoder.yudao.module.system.controller.admin.template.vo.PersonalTemplateUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.template.PersonalTemplate;
import cn.iocoder.yudao.module.system.dal.dataobject.template.SystemTemplate;
import cn.iocoder.yudao.module.system.dal.dataobject.template.Template;
import cn.iocoder.yudao.module.system.dal.mysql.template.PersonalTemplateMapper;
import cn.iocoder.yudao.module.system.dal.mysql.template.SystemTemplateMapper;
import cn.iocoder.yudao.module.system.dal.mysql.template.TemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TemplateServiceImpl implements TemplateService{

    @Autowired
    SystemTemplateMapper systemTemplateMapper;
    @Autowired
    PersonalTemplateMapper personalTemplateMapper;
    @Autowired
    TemplateMapper templateMapper;

    @Override
    public List<SystemTemplate> getALLSystemTemplate() {
        return systemTemplateMapper.selectList();
    }

    @Override
    public List<PersonalTemplateRespVO> getALLPersonalTemplate() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        List<PersonalTemplate> templates = personalTemplateMapper.selectList("creator", loginUser.getId());
        return BeanUtil.copyToList(templates,PersonalTemplateRespVO.class);
    }

    @Override
    public List<Template> getTemplateList() {
        return templateMapper.selectList();
    }

    @Override
    public PersonalTemplateRespVO getPersonalTemplateById(Long id) {
        PersonalTemplate personalTemplate = personalTemplateMapper.selectOne("id", id);
        return BeanUtil.toBean(personalTemplate,PersonalTemplateRespVO.class);
    }

    @Override
    public void addPersonalTemplate(PersonalTemplateCreateReqVO vo) {
        PersonalTemplate personalTemplate = BeanUtil.toBean(vo, PersonalTemplate.class);
        personalTemplateMapper.insert(personalTemplate);
    }

    @Override
    public void updatePersonalTemplate(PersonalTemplateUpdateReqVO vo) {
        PersonalTemplate personalTemplate = BeanUtil.toBean(vo, PersonalTemplate.class);
        personalTemplateMapper.updateById(personalTemplate);
    }

    @Override
    public void removePersonalTemplateById(Long id) {
        personalTemplateMapper.deleteById(id);
    }
}
